package com.suray.rgv60.manual;

import com.suray.basic.wcs.utils.LoggerType;
import com.suray.basic.wcs.utils.ModuleType;
import com.suray.basic.wcs.utils.thread.BaseThread;
import com.suray.rgv.util.RgvLogger;
import com.suray.rgv60.util.ManualMoveType;
import suray.rgv.communication.SeerCommManage;
import suray.rgv.communication.data.OpenCircularMove;

import java.io.IOException;

/**
 * 手动行驶
 */
public class ManualMove extends BaseThread {

    /**
     * 通讯
     */
    private SeerCommManage seerComm;

    /**
     * 手动行驶类型
     */
    private volatile ManualMoveType moveType;

    /**
     * 序列号
     */
    private String series;

    public ManualMove(String series, SeerCommManage seerComm) {
        super("手动行驶", 100, ModuleType.RGV_60);
        this.seerComm = seerComm;
        this.series = series;
        this.moveType = ManualMoveType.INIT;

        this.start();
    }

    @Override
    protected void process() {
        try {
            switch (moveType) {
                case INVALID:
                    changeManualMove(ManualMoveType.INIT);
                    break;
                case INIT:
                    break;
                case FORWARD:
                    forwardRun();
                    break;
                case FALL_BACK:
                    fallBackRun();
                    break;
                case TURN_LEFT:
                    turnLeft();
                    break;
                case TURN_RIGHT:
                    turnRight();
                    break;
                case STOP_MOVE:
                    stopManualMove();
                    break;
                default:
                    RgvLogger.info(LoggerType.MOVE_BY_SPEED, series, "手动行驶",
                            "不支持的操作:" + moveType.toString(), this.getClass());
                    break;
            }
        } catch (IOException e) {
            RgvLogger.info(LoggerType.MOVE_BY_SPEED, series, "手动行驶", "通讯异常", this.getClass());
        }
    }

    public synchronized void changeManualMove(ManualMoveType moveType) {
        RgvLogger.info(LoggerType.MOVE_BY_SPEED, series, "手动行驶", moveType.toString(), this.getClass());
        this.moveType = moveType;
    }


    /**
     * 前进
     */
    private void forwardRun() throws IOException {
        OpenCircularMove openCircularMove = new OpenCircularMove();
        openCircularMove.setVx(0.2);
        openCircularMove.setDuration(200);
        seerComm.motionReq(openCircularMove);
    }

    /**
     * 后退
     */
    private void fallBackRun() throws IOException {
        OpenCircularMove openCircularMove = new OpenCircularMove();
        openCircularMove.setVx(-0.2);
        openCircularMove.setDuration(200);
        seerComm.motionReq(openCircularMove);
    }

    /**
     * 左转
     */
    private void turnLeft() throws IOException {
        OpenCircularMove openCircularMove = new OpenCircularMove();
        openCircularMove.setW(1);
        openCircularMove.setDuration(200);
        seerComm.motionReq(openCircularMove);
    }

    /**
     * 右转
     */
    private void turnRight() throws IOException {
        OpenCircularMove openCircularMove = new OpenCircularMove();
        openCircularMove.setW(-1);
        openCircularMove.setDuration(200);
        seerComm.motionReq(openCircularMove);
    }

    /**
     * 停止手动行驶
     */
    private void stopManualMove() {
        try {
            OpenCircularMove openCircularMove = new OpenCircularMove();
            openCircularMove.setDuration(0);
            seerComm.motionReq(openCircularMove);
        } catch (IOException e) {
            RgvLogger.exceptionLog(LoggerType.ERROR, series, "停止手动行驶失败", e, this.getClass());
        }
        changeManualMove(ManualMoveType.INIT);
    }
}
